package concurrent.mysynchronized;

/**
 * @package concurrent.mysynchronized
 * @Class SynchronizedObjectCodeBlock2
 * @Description TODO
 * @Author zhangxinhua
 * @Date 20-1-9 上午11:27
 */
public class SynchronizedObjectCodeBlock2 implements Runnable {
    static SynchronizedObjectCodeBlock2 synchronizedObjectCodeBlock2 = new SynchronizedObjectCodeBlock2();
    static int i = 0;
    Object lock1 = new Object();
    Object lock2 = new Object();
    @Override
    public void run() {
        synchronized (lock1) {
            System.out.println("我是lock1对象锁的代码块形式，我叫"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"_lock1运行结束。");
        }
        synchronized (lock2) {
            System.out.println("我是lock2对象锁的代码块形式，我叫"+Thread.currentThread().getName());
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"_lock2运行结束。");
        }
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(synchronizedObjectCodeBlock2);
        Thread t2 = new Thread(synchronizedObjectCodeBlock2);
        t1.start();
        t2.start();
        while (t1.isAlive() || t2.isAlive()) {

        }
        System.out.println("finished:");
    }
}
